
/**
 * 
 * 删除有序链表中重复的元素2
 * 
 * 
 * 
 */
public class 删除有序链表中重复的元素2 {
    
    public static void main(String[] args) {
        
        ListNode node = new ListNode(1);
        node.next = new ListNode(1);
        node.next.next = new ListNode(2);
        node.next.next.next = new ListNode(3);
        node.next.next.next.next = new ListNode(3);

        ListNode pre = deleteDuplicates(node);

        System.out.println(pre.toString());
        

    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public static ListNode deleteDuplicates (ListNode head) {
        // write code here

        if (head == null || head.next == null) {
            return head;
        }
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode prev = dummy;
        ListNode curr = head;
        while (curr!= null) {
            boolean isDuplicate = false;
            while (curr.next!= null && curr.val == curr.next.val) {
                curr = curr.next;
                isDuplicate = true;
            }
            if (isDuplicate) {
                prev.next = curr.next;
            } else {
                prev = prev.next;
            }
            curr = curr.next;
        }
        return dummy.next;
    }
}
